package net.libra.scrollui
{
	
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	
	import net.libra.ui.EasyButton;
	import net.libra.ui.base.BaseSprite;
	

	/**
	 *
	 *@author elvisjiang
	 *@createTime 2011-7-18
	 */
	public class VScollBar extends BaseSprite
	{
		private var _buttonDown:EasyButton;
		private var _buttonUp:EasyButton;
		private var _everyClickHeight:Number;
		private var _skin:DisplayObjectContainer;
		private var _slider:VSlider;
		public function VScollBar(skin:DisplayObjectContainer,buttonUp:EasyButton,buttonDown:EasyButton,everyClickHeight:Number,slider:VSlider)
		{
			_skin = skin;
			_buttonUp = buttonUp;
			_buttonDown = buttonDown;
			_everyClickHeight = everyClickHeight;
			_slider = slider;
			initDisplay();
			initEvent();
		}
		protected function initDisplay():void
		{
			this.addChild(_skin);
			_skin.addChild(_buttonUp);
			_skin.addChild(_buttonDown);
			this.mouseChildren = true;
		}
		override public function destroy():void
		{
			_skin.removeChild(_buttonUp);
			_skin.removeChild(_buttonDown);
			
//			_slider.destroy();
			_slider = null;
			
			_buttonUp.removeEventListener(MouseEvent.CLICK,button_up);
//			_buttonUp.destory();
			_buttonUp = null;
			_buttonDown.removeEventListener( MouseEvent.CLICK,button_down);
//			_buttonDown.destory();
			_buttonDown = null;
			
			_skin = null;
			super.destroy();
		}
		protected function initEvent():void
		{
			_buttonUp.addEventListener( MouseEvent.CLICK,button_up);
			_buttonDown.addEventListener( MouseEvent.CLICK,button_down);
		}
		public function button_up(evt:MouseEvent = null):void
		{
			if(_slider.button.y <= 0){
				return;
			}
			
			if(_slider.button.y - _everyClickHeight <= 0){
				_slider.button.y = 0;
			}else{
				_slider.button.y -= _everyClickHeight;
			}
			
			dispatchEvent(new ListEvent(ListEvent.SCROLL,_slider.button.y));
		}
		public function button_down(evt:MouseEvent = null):void
		{
			if(!_slider.isEnabedScroll()){
				return;
			}
			if(_slider.button.y + int(_slider.button.height) >= _slider.trackHeight){
				return;
			}
			
			var tailY:Number = int(_slider.trackHeight - _slider.button.height);
			
			if(_slider.button.y + _everyClickHeight >= tailY){
				_slider.button.y  =  tailY;
			}else{
				_slider.button.y += _everyClickHeight;
			}
			
			dispatchEvent(new ListEvent(ListEvent.SCROLL,_slider.button.y));
		}
	}
}